package com.scwatch.core.web.back;

import com.scwatch.common.orm.RowSide;
import com.scwatch.common.web.PathResolver;
import com.scwatch.common.web.Servlets;
import com.scwatch.core.constant.Constants;
import com.scwatch.core.domain.PublishPoint;
import com.scwatch.core.domain.Site;
import com.scwatch.core.service.OperationLogService;
import com.scwatch.core.service.OrgService;
import com.scwatch.core.service.PublishPointService;
import com.scwatch.core.service.SiteService;
import com.scwatch.core.support.Context;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.scwatch.core.constant.Constants.*;

/**
 * 发布点管理
 * @author  gjl
 *
 */
@Controller
@RequestMapping("/core/pub_point")
@RequiresRoles("super")
public class PubPointController {
	private static final Logger logger = LoggerFactory.getLogger(PubPointController.class);

    /**
     * 发布点查询
     * @param pageable 分页对象
     * @param request
     * @param modelMap
     * @return 结果页面
     */
	@RequestMapping("list.do")
	@RequiresRoles("super")
	@RequiresPermissions("core:publish_point:list")
	public String list(@PageableDefault(sort = "id") Pageable pageable, HttpServletRequest request,
			org.springframework.ui.Model modelMap) {
		Map<String, String[]> params = Servlets.getParamValuesMap(request, Constants.SEARCH_PREFIX);
		List<PublishPoint> list = publishPointService.findList(params, pageable.getSort());
		modelMap.addAttribute("list", list);
		return "core/pub_point/pubpoint_list";
}

	@RequestMapping("create.do")
	@RequiresRoles("super")
	@RequiresPermissions("core:publish_point:create")
	public String create(Integer id, HttpServletRequest request, org.springframework.ui.Model modelMap) {
        if (id != null) {
            PublishPoint bean = publishPointService.get(id);
            modelMap.addAttribute("bean", bean);
        }
        modelMap.addAttribute(OPRT, CREATE);
		return "core/pub_point/pubpoint_form";
	}
    @RequiresPermissions("core:publish_point:edit")
    @RequiresRoles("super")
    @RequestMapping("edit.do")
    public String edit(
            Integer id,
            Integer position,
            @PageableDefault(sort = "id") Pageable pageable,
            HttpServletRequest request, org.springframework.ui.Model modelMap) {
        PublishPoint bean = publishPointService.get(id);
        Map<String, String[]> params = Servlets.getParamValuesMap(request,
                Constants.SEARCH_PREFIX);
        RowSide<PublishPoint> side = publishPointService.findSide(params, bean,
                position, pageable.getSort());
        modelMap.addAttribute("bean", bean);
        modelMap.addAttribute("side", side);
        modelMap.addAttribute("position", position);
        modelMap.addAttribute(OPRT, EDIT);
        return "core/pub_point/pubpoint_form";
    }

    /**
     * 保存发布点
     * @param bean 发布点对象
     * @param redirect
     * @param request
     * @param ra
     * @return 结果页面
     */
    @RequiresPermissions("core:publish_point:save")
    @RequiresRoles("super")
    @RequestMapping("save.do")
    public String save(PublishPoint bean, String redirect, HttpServletRequest request,
                       RedirectAttributes ra) {
        Site site = Context.getCurrentSite();
        bean.setGlobal(site.getGlobal());
        publishPointService.save(bean);
        logService.operation("opr.publishPoint.add", bean.getName(), null, bean.getId(),
                request);
        logger.info("save PublishPoint, name={}.", bean.getName());
        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);
        if (Constants.REDIRECT_LIST.equals(redirect)) {
            return "redirect:list.do";
        } else if (Constants.REDIRECT_CREATE.equals(redirect)) {
            return "redirect:create.do";
        } else {
            ra.addAttribute("id", bean.getId());
            return "redirect:edit.do";
        }
    }


    /**
     * 修改发布点
     * @param bean 发布点对象
     * @param position 数据位置
     * @param redirect
     * @param request
     * @param ra
     * @return 结果页面
     */
    @RequiresPermissions("publish_point:update")
    @RequiresRoles("super")
    @RequestMapping("update.do")
    public String update(PublishPoint bean, Integer position,
                         String redirect, HttpServletRequest request, RedirectAttributes ra) {
        Site site = Context.getCurrentSite();
//        Backends.validateDataInSite(bean, site.getId());
        bean.setGlobal(site.getGlobal());
        publishPointService.update(bean);
        logService.operation("opr.publishPoint.edit", bean.getName(), null,
                bean.getId(), request);
        logger.info("update PublishPoint, name={}.", bean.getName());
        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);
        if (Constants.REDIRECT_LIST.equals(redirect)) {
            return "redirect:list.do";
        } else {
            ra.addAttribute("id", bean.getId());
            ra.addAttribute("position", position);
            return "redirect:edit.do";
        }
    }

    /**
     * 删除发布点
     * @param ids 发布点id
     * @param request
     * @param ra
     * @return 结果页面
     */
	@RequestMapping("delete.do")
	@RequiresRoles("super")
	@RequiresPermissions("core:publish_point:delete")
	public String delete(Integer[] ids, HttpServletRequest request, RedirectAttributes ra) {
        PublishPoint[] beans = publishPointService.delete(ids);
        for (PublishPoint bean : beans) {
            logService.operation("opr.publishPoint.delete", bean.getName(), null,
                    bean.getId(), request);
            logger.info("delete PublishPoint, name={}.", bean.getName());
        }
        ra.addFlashAttribute(MESSAGE, DELETE_SUCCESS);
        return "redirect:list.do";
	}

	@ModelAttribute("bean")
	public Site preloadBean(@RequestParam(required = false) Integer oid) {
		return oid != null ? service.get(oid) : null;
	}

	@Autowired
	private OperationLogService logService;
	@Autowired
	private PublishPointService publishPointService;
	@Autowired
	private OrgService orgService;
	@Autowired
	private SiteService service;

	@Autowired
	private PathResolver pathResolver;
}
